package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.HashMap;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.bean.TriggerBean;
import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/8/14.
 */
public class RuleDeviceAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<String> items;
    private HashMap<String, TriggerBean> triggerBeans;
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
    private View.OnClickListener onClickListener;

    /**
     * 构造函数
     *
     * @param context
     * @param devices      设置规则界面显示的触发类设备
     * @param triggerBeans 选择的触发条件
     */
    public RuleDeviceAdapter(Context context, ArrayList<String> devices, HashMap<String, TriggerBean> triggerBeans) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        items = devices;
        this.triggerBeans = triggerBeans;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_auto_device, null);
            holder.cbChecked = (CheckBox) convertView.findViewById(R.id.cb_checked);
            holder.ivDeviceIcon = (ImageView) convertView.findViewById(R.id.iv_device_icon);
            holder.tvDeviceName = (TextView) convertView.findViewById(R.id.tv_device_name);
            holder.tvModify = (TextView) convertView.findViewById(R.id.tv_modify);
            holder.tvConditionTitle = (TextView) convertView.findViewById(R.id.tv_condition_title);
            holder.tvCondition = (TextView) convertView.findViewById(R.id.tv_condition);
            holder.rlEditValue = (RelativeLayout) convertView.findViewById(R.id.rl_edit_value);
            holder.rlCheckBox = (RelativeLayout) convertView.findViewById(R.id.rl_checkbox);
            convertView.setTag(holder);
            AutoUtils.auto(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (triggerBeans == null) {
            triggerBeans = new HashMap<String, TriggerBean>();
        }
        DeviceBean deviceBean = null;
        deviceBean = SystemConstant.deviceBeanHashMap.get(items.get(position));
        holder.tvDeviceName.setText(deviceBean.getName());
        holder.cbChecked.setTag(deviceBean);
        if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
            holder.ivDeviceIcon.setImageResource(R.drawable.ph_icon);
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
            holder.ivDeviceIcon.setImageResource(R.drawable.temperature_icon);
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
            holder.ivDeviceIcon.setImageResource(R.drawable.water_lever_icon);
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
            holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
        } else {
            holder.ivDeviceIcon.setImageResource(R.drawable.ictest);
        }
        if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
            holder.tvConditionTitle.setText(mContext.getString(R.string.fragment_rule_device_triiger_condition));
        } else {
            holder.tvConditionTitle.setText(mContext.getString(R.string.fragment_rule_device_action));
        }
        TriggerBean triggerBean = triggerBeans.get(deviceBean.getAddress());
        if (triggerBean == null) {
            holder.cbChecked.setChecked(false);
            holder.tvCondition.setText(mContext.getString(R.string.listview_value_unset));
        } else {
            holder.cbChecked.setChecked(true);
            holder.tvCondition.setText(triggerBean.toString());
        }
        if (holder.cbChecked.isChecked()) {
            holder.rlEditValue.setVisibility(View.VISIBLE);
            holder.tvModify.setVisibility(View.VISIBLE);
        } else {
            holder.rlEditValue.setVisibility(View.GONE);
            holder.tvModify.setVisibility(View.GONE);
            holder.tvCondition.setText(mContext.getString(R.string.listview_value_unset));
        }
        holder.rlEditValue.setTag(triggerBean);
        if (onClickListener != null)
            holder.rlEditValue.setOnClickListener(onClickListener);
        holder.rlCheckBox.setTag(holder.cbChecked);
        if (onClickListener != null)
            holder.rlCheckBox.setOnClickListener(onClickListener);
        if (onCheckedChangeListener != null)
            holder.cbChecked.setOnCheckedChangeListener(onCheckedChangeListener);

        return convertView;
    }


    //设置勾选框改变监听事件
    public void setCheckedChangerListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    //更新数据
    public void update(HashMap<String, TriggerBean> triggerBeanHashMap) {
        triggerBeans = triggerBeanHashMap;
        notifyDataSetChanged();
    }

    //设置修改模式监听函数
    public void setOnModifyClickListener(View.OnClickListener onModifyClickListener) {
        this.onClickListener = onModifyClickListener;
    }


    static class ViewHolder {
        CheckBox cbChecked;
        ImageView ivDeviceIcon;
        TextView tvDeviceName;
        TextView tvModify;
        TextView tvConditionTitle;
        TextView tvCondition;
        RelativeLayout rlEditValue;
        RelativeLayout rlCheckBox;
    }
}
